﻿
@{
    ViewBag.Title = "调拨收货";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";

    var allocateNo = ViewBag.AllocateNo as string;
}

@section Contents{
    <style>
        #dialogForm .layui-form-label {
            width: 110px;
        }

        #dialogForm .layui-form-item .layui-input-inline {
            width: 230px;
        }
    </style>
}

<div id="page"></div>

<script type="text/template" id="tplPage">
    <div>
        <div class="layui-fluid">
            <div class="layui-card">
                <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">{{lang.Field.AllocateOrderNo}}：</label>
                            <div class="layui-input-inline">
                                <input id="txtAllocateNo" type="text" placeholder="请扫描调拨单号" autocomplete="off" class="layui-input" value="@allocateNo" @(!string.IsNullOrWhiteSpace(allocateNo) ? "readonly" : "")>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button class="layui-btn layuiadmin-btn-list" type="button" id="btnRefresh">
                                <i class="layui-icon layui-icon-refresh layuiadmin-button-btn"></i>{{lang.Field.ReReceive}}
                            </button>
                            <button class="layui-btn layuiadmin-btn-list" type="button" id="btnStartScan">
                                <i class="layui-icon layui-icon-app layuiadmin-button-btn"></i>{{lang.Field.ScanReceive}}
                            </button>
                            <button class="layui-btn layuiadmin-btn-list" type="button" id="btnSubmit">
                                <i class="layui-icon layui-icon-ok layuiadmin-button-btn"></i>{{lang.Field.SubmitReceive}}
                            </button>
                        </div>
                    </div>
                </div>
            </div>

            <div class="layui-card">
                <div class="layui-card-body">
                    <table id="table" lay-filter="table"></table>
                </div>
            </div>
        </div>

        <div id="dialog" style="display:none;">
            <div class="layui-form" id="dialogForm" lay-filter="layuiadmin-app-form-list" style="padding: 20px 30px;">
                <div class="layui-form-item">
                    <label class="layui-form-label">{{lang.Field.BatchNo}}：</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" name="BatchNo" placeholder="请扫描批次号" id="txtBatchNo">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">{{lang.Field.MaterialCode}}：</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" name="ProductCode" readonly>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">{{lang.Field.MaterialName}}：</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" name="ProductName" readonly>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">{{lang.Field.ShipQuantity}}：</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" name="Quantity" readonly>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label"><span style="color:red;">*</span>{{lang.Field.CurReceiveQuantity}}：</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" lay-verify="required" name="ReceiveQuantity">
                    </div>
                </div>
                <div class="layui-form-item" style="text-align:center;padding-top: 20px;">
                    <button class="layui-btn layui-btn-normal" lay-submit lay-filter="receiveProduct"><i class="layui-icon">&#xe605;</i>{{lang.Field.ReceiveMaterial}}</button>
                </div>
            </div>
        </div>
    </div>
</script>


@section Scripts{
    <script>
        var products = new Array();
        var vm = new Vue({
            el: "#page",
            template: "#tplPage",
            data: {
                lang: JSON.parse(lang).package
            }
        });

        setTimeout(function () {
            initLayui();
        }, 50);
        function initLayui() {
            layui.use(['table', 'layer', 'form'], function () {
                var table = layui.table,
                    form = layui.form,
                    layer = layui.layer;
                var options = {
                    elem: '#table'
                    , id: "table"
                    , cols: [[
                        { title: '序号', width: 80, type: "numbers" }
                        , { field: 'ProductCode', title: '物料编码', minWidth: 200 }
                        , { field: 'ProductName', title: '物料名称', minWidth: 200 }
                        , { field: 'BatchNo', title: '批次号', minWidth: 200 }
                        , { field: 'Quantity', title: '发货数量', fixed: 'right', minWidth: 80 }
                        , { field: 'ReceiveQuantity', title: '本次收货', fixed: 'right', minWidth: 70 }
                        , {
                            title: '操作', width: 100, fixed: 'right', templet: function (d) {
                                var str = '<button class="layui-btn layui-btn-sm layui-btn-danger delProductCode" code="' + d.ProductCode + '"><i class="layui-icon">&#xe640;</i>删除</button>';
                                return str;
                            }
                        }
                    ]]
                    , data: products
                    , limit: products.length
                    , even: true
                };
                table.render(options);

                $("#btnRefresh").click(function () {
                    window.location = "/AllocateOrder/AllocateInbound";
                });
                $("#btnStartScan").click(function () {
                    layer.open({
                        type: 1,
                        area: ['500px', '430px'],
                        content: $('#dialog').html()
                    });
                });
                form.on('submit(receiveProduct)', function (data) {
                    var exist = false;
                    var receiveQuantity = data.field.ReceiveQuantity == "" ? 0 : parseFloat(data.field.ReceiveQuantity);
                    if (receiveQuantity <= 0) {
                        layer.msg("请输入收货数量");
                        return;
                    }
                    for (var i = 0; i < products.length; i++) {
                        if (products[i].ProductCode == data.field.ProductCode && products[i].BatchNo == data.field.BatchNo) {
                            products[i].ReceiveQuantity = products[i].ReceiveQuantity + receiveQuantity;
                            exist = true;
                        }
                    }
                    if (!exist) {
                        products.splice(0, 0, {
                            ProductCode: data.field.ProductCode,
                            ProductName: data.field.ProductName,
                            BatchNo: data.field.BatchNo,
                            Quantity: data.field.Quantity,
                            ReceiveQuantity: receiveQuantity
                        });
                    }

                    options.data = products;
                    options.limit = products.length;
                    layui.table.reload("table", options);

                    $("input[name=BatchNo]").val("");
                    $("input[name=ProductCode]").val("");
                    $("input[name=ProductName]").val("");
                    $("input[name=Quantity]").val("");
                    $("input[name=ReceiveQuantity]").val(0);
                });

                $(document).on("keyup", "#txtAllocateNo", function (e) {
                    if (e.keyCode == 13 && $(this).val().trim() != "") {
                        $(this).attr("readonly", "readonly");
                    }
                });

                $(document).on("keyup", "#txtBatchNo", function (e) {
                    if (e.keyCode == 13) {
                        var allocateNo = $("#txtAllocateNo").val().trim();
                        var batchNo = $("#txtBatchNo").val().trim();
                        if (batchNo == "" || allocateNo == "") {
                            return;
                        }
                        var loadId = layer.load();
                        $.ajax({
                            url: "/AllocateOrder/GetAllocateProduct",
                            data: {
                                allcateNo: allocateNo,
                                batchNo: batchNo
                            },
                            complete: function () {
                                layer.close(loadId);
                            },
                            success: function (res) {
                                if (res.ErrCode != 200) {
                                    layer.msg(res.ErrMsg, { icon: 5 });
                                    return;
                                } else if (res.Data == null) {
                                    layer.msg("批次号不存在", { icon: 5 });
                                    return;
                                }
                                $("input[name=ProductCode]").val(res.Data.ProductCode);
                                $("input[name=ProductName]").val(res.Data.ProductName);
                                $("input[name=Quantity]").val(res.Data.Quantity);
                                $("input[name=ReceiveQuantity]").val(0);
                            }
                        });
                    }
                });

                $(document).on("click", ".delProductCode", function () {
                    var productCode = $(this).attr("code").trim();
                    for (var i = 0; i < products.length; i++) {
                        if (products[i].ProductCode == productCode) {
                            products.splice(i, 1);
                            break;
                        }
                    }

                    options.data = products;
                    options.limit = products.length;
                    layui.table.reload("table", options);
                });

                $("#btnSubmit").click(function () {
                    if (products.length <= 0) {
                        layer.msg("请至少收一件物料", { icon: 5 });
                        return;
                    }
                    var loadId = layer.load();
                    $.ajax({
                        url: "/AllocateOrder/AllocateInbound",
                        data: {
                            allocateNo: $("#txtAllocateNo").val().trim(),
                            products: products
                        },
                        complete: function () {
                            layer.close(loadId);
                        },
                        success: function (res) {
                            if (res.ErrCode != 200) {
                                layer.msg(res.ErrMsg, { icon: 5 });
                                return;
                            }
                            layer.msg("收货成功！", function () {
                                top.layui.admin.closeThisTabs();
                            });
                        }
                    });
                });
            });
        }
    </script>
}
